// pages/cart/component/cart.js
import { addCartOrCahngeGoodCountAPI } from "@/api/cartAPI"
import cartBehaviors from "@/behaviors/cartBehaviors"
import userBehaviors from "@/behaviors/userBehaviors"
import cartStore from "@/store/cartStore"
import userStore from "@/store/userStore"
import { CartItem } from "@/type/cartTypes"
Page({
  behaviors: [userBehaviors, cartBehaviors],
  /**
   * 
   * @description 组件的属性列表
   */
  properties: {},

  /**
   * 
   * @description 组件的初始数据
   */
  data: {
    emptyDes: '还没有添加商品，快去添加吧',
    token: ""
  },
  /**
   * 
   * @description 组件的方法列表
   */

  /**
   * 
   *@description 获取购物车列表的回调函数 
   */
  async fetchCartList() {
    try {
      const fetchResult: any = await cartStore.fetchCartList()
      wx.showToast({ title: fetchResult, icon: "success" })
    } catch (error) {
      wx.showToast({ title: error.message, icon: "error" })
    }
  },
  /**
   * 
   * @description 点击删除按钮的回调函数
   */
  async deleteCartGood(event: WechatMiniprogram.BaseEvent) {
    try {
      const { goodsid } = event.currentTarget.dataset
      const deleteResult: any = await cartStore.deleteCart(goodsid)
      wx.showToast({ title: deleteResult, icon: "success" })
    } catch (error) {
      wx.showToast({ title: error.message, icon: "error" })
    }
  },

  /**
   *@description 勾选商品状态的回调函数 
   */
  async changeGoodChecked(event: any) {
    const { good }: { good: CartItem } = event.currentTarget.dataset
    const { detail }: { detail: boolean } = event
    const isChecked: 0 | 1 = detail ? 1 : 0
    try {
      const changeResult: any = await cartStore.changeGoodChecked(good.goodsId as 1 | 0, isChecked)
      wx.showToast({ title: changeResult, icon: "success" })
    } catch (error) {
      wx.showToast({ title: error.message, icon: "error" })
    }
  },
  /**
   *@description 全选和取消全选的回调函数 
   */
  async changeAllChecked(event: any) {
    const { detail }: { detail: boolean } = event
    const isChecked: 0 | 1 = detail ? 1 : 0
    try {
      const changeResult: any = await cartStore.changeGoodAllChecked(isChecked)
      wx.showToast({ title: changeResult, icon: "success" })
    } catch (error) {
      wx.showToast({ title: error.message, icon: "error" })
    }
  },
  /**
 *@description 商品数量发生变化的回调函数
 */
  async onChangeCount(event: any) {
    const { good }: { good: CartItem } = event.currentTarget.dataset
    const { detail } = event
    try {
      const info = { goodsId: good.goodsId, count: detail - good.count, blessing: good.blessing }
      await addCartOrCahngeGoodCountAPI(info)
      cartStore.fetchCartList()
    } catch (error) {
      wx.showToast({ title: error.message, icon: "error" })
    }
  },
  /**
   * 
   * 跳转到商品详情的回调函数
   */
  goToDetail(event: WechatMiniprogram.BaseEvent) {
    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/subPackages/detail/detail?goodsId=${id}`
    })
  },
  /**
   * 
   * @description 去结算的回调函数
   */
  goToOrderPay() {
    const some: boolean = cartStore.cartList.some((item: CartItem) => item.isChecked === 1)
    if (some) {
      wx.navigateTo({
        url: "/subPackages/order/detail/detail"
      })
    } else {
      wx.showToast({ title: "请选择商品", icon: "error" })
    }
  },
  onShow() {
    this.fetchCartList()
    this.setData({
      token: userStore.token
    })
  }
})
